﻿using System;
using System.ComponentModel;
using System.Security;
using System.Windows;
using System.Windows.Input;
using Co9320.NamaAlert.Extension.Command;
using Co9320.NamaAlert.Extension.Model;

namespace Co9320.NamaAlert.Extension.ViewModel
{
    class OptionViewModel : INotifyPropertyChanged
    {

        private ISetting setting;
        public OptionViewModel(ISetting setting)
        {
            this.setting = setting;

            this.FromMailAddress = setting.FromMailAddress;
            this.ToMailAddress = setting.ToMailAddress;
            this.SmtpServer = setting.SmtpServer;
            this.SmtpPort = setting.SmtpPort;
            this.SmtpUserName = setting.SmtpUserName;
            this.SmtpPassword = setting.SmtpPassword;
            this.SmtpUseSsl = setting.SmtpUseSsl;
            this.TextEncoding = setting.TextEncoding;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }

        #region プロパティ
        /// <summary>
        /// 送信元メールアドレス
        /// </summary>
        public string FromMailAddress
        {
            get;
            set;
        }

        /// <summary>
        /// 送信先メールアドレス
        /// </summary>
        public string ToMailAddress
        {
            get;
            set;
        }

        /// <summary>
        /// 文字エンコーディング
        /// </summary>
        public string TextEncoding
        {
            get;
            set;
        }

        #region SMTP設定
        /// <summary>
        /// SMTPサーバーアドレス
        /// </summary>
        public string SmtpServer
        {
            get;
            set;
        }

        /// <summary>
        /// SMTPサーバーポート
        /// </summary>
        public int SmtpPort
        {
            get;
            set;
        }


        /// <summary>
        /// SMTPでユーザー認証を行うかどうか
        /// </summary>
        public bool SmtpUseAuth
        {
            get { return (SmtpUserName != "" && SmtpPassword.Length != 0); }
        }

        /// <summary>
        /// SMTP認証ユーザー名
        /// </summary>
        public string SmtpUserName
        {
            get;
            set;
        }

        /// <summary>
        /// SMTP認証パスワード
        /// </summary>
        public SecureString SmtpPassword
        {
            get;
            set;
        }

        /// <summary>
        /// SMTPでSSLを使うかどうか
        /// </summary>
        public bool SmtpUseSsl
        {
            get;
            set;
        }
        #endregion
        #endregion

        private RelayCommand _testSendCommand;
        bool _testSending = false;
        public ICommand TestSendCommand
        {
            get
            {
                if (_testSendCommand == null)
                {
                    _testSendCommand = new RelayCommand(param =>
                    {
                        if (this.ToMailAddress == "" || this.FromMailAddress == "" || this.SmtpServer == "")
                        {
                            MessageBox.Show("メール送信に失敗しました。\n必須項目が設定されていません。");
                        }
                        else
                        {
                            _testSending = true;
                            TestSetting testSend = new TestSetting();
                            this.save(testSend);
                            try
                            {
                                new SendMail(testSend).Send("テスト送信", "テスト送信", e =>
                                {
                                    _testSending = false;
                                    if (e.Error != null)
                                        MessageBox.Show("メール送信に失敗しました。\n" + e.Error.Message);
                                    else
                                        MessageBox.Show("メール送信に成功しました");
                                });
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show("メール送信に失敗しました。\n" + e.Message);
                                _testSending = false;
                            }
                        }
                    }, param => (this.ToMailAddress == "" || this.FromMailAddress == "" || this.SmtpServer != "") && !this._testSending);
                }
                return _testSendCommand;
            }

        }

        public void Save()
        {
            this.save(this.setting);
        }

        private void save()
        {
            this.save(this.setting);
        }
        private void save(ISetting setting)
        {
            setting.FromMailAddress = this.FromMailAddress;
            setting.ToMailAddress = this.ToMailAddress;
            setting.SmtpServer = this.SmtpServer;
            setting.SmtpPort = this.SmtpPort;
            setting.SmtpUserName = this.SmtpUserName;
            setting.SmtpPassword = this.SmtpPassword;
            setting.SmtpUseSsl = this.SmtpUseSsl;
            setting.TextEncoding = this.TextEncoding;
        }
    }
}
